<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务代码" prop="itemCode">
        <el-input
                v-model="queryParams.itemCode"
                placeholder="请输入服务项目代码"
                clearable
                @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务名称" prop="itemName">
        <el-input
                v-model="queryParams.itemName"
                placeholder="请输入服务项目名称"
                clearable
                @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
                v-hasPermi="['system:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="info"
                plain
                icon="Sort"
                @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="warning"
                plain
                icon="Download"
                @click="handleExport"
                v-hasPermi="['system:item:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
            v-if="refreshTable"
            v-loading="loading"
            :data="itemList"
            row-key="id"
            :default-expand-all="isExpandAll"
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column label="服务项目代码" align="center" prop="itemCode" />
      <el-table-column label="服务项目名称" align="center" prop="itemName" />
      <el-table-column label="上级目录" align="center" prop="parentName" />
      <el-table-column label="分类等级" align="center" prop="levelId" />
      <el-table-column label="组内排序号" align="center" prop="levelOrder" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:item:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:item:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改配置-证书目录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="itemRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务代码" prop="itemCode">
          <el-input v-model="form.itemCode" :disabled="mode === 'update'" placeholder="请输入服务项目代码" />
        </el-form-item>
        <el-form-item label="服务名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入服务项目名称" />
        </el-form-item>
        <el-form-item label="上级目录" prop="parentId">
          <el-tree-select
                  v-model="form.parentId"
                  :data="itemOptions"
                  :props="{ value: 'itemCode', label: 'itemName', children: 'children' }"
                  value-key="itemCode"
                  placeholder="选择上级目录"
                  check-strictly
          />
        </el-form-item>
        <el-form-item label="分类等级" prop="levelId">
          <el-input v-model="form.levelId" placeholder="请输入分类等级" />
        </el-form-item>
        <el-form-item label="组内排序" prop="levelOrder">
          <el-input v-model="form.levelOrder" placeholder="请输入组内排序号" />
        </el-form-item>
<!--        <el-form-item label="图标" prop="icon">-->
<!--          <el-input v-model="form.icon" placeholder="请输入图标" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否叶子节点 0-不是 1-是" prop="isLeaf">-->
<!--          <el-input v-model="form.isLeaf" placeholder="请输入是否叶子节点 0-不是 1-是" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否展示到首页" prop="isShow">-->
<!--          <el-input v-model="form.isShow" placeholder="请输入是否展示到首页" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="路由" prop="route">-->
<!--          <el-input v-model="form.route" placeholder="请输入路由" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="路径code" prop="pathCode">-->
<!--          <el-input v-model="form.pathCode" placeholder="请输入路径code" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="路径名称" prop="pathName">-->
<!--          <el-input v-model="form.pathName" placeholder="请输入路径名称" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Item">
  import { listItem, getItem, delItem, addItem, updateItem } from "@/api/cer/cerItem";

  const { proxy } = getCurrentInstance();

  const itemList = ref([]);
  const itemOptions = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const isExpandAll = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const mode = ref('add');
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  const refreshTable = ref(true);

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      itemCode: null,
      itemName: null,
      parentId: null,
      levelId: null,
      levelOrder: null,
      icon: null,
      isLeaf: null,
      isShow: null,
      route: null,
      pathCode: null,
      pathName: null,
    },
    rules: {
      itemCode: [
        { required: true, message: "服务项目代码不能为空", trigger: "blur" }
      ],
      itemName: [
        { required: true, message: "服务项目名称不能为空", trigger: "blur" }
      ],
      parentId: [
        { required: true, message: "上级id不能为空", trigger: "blur" }
      ],
      levelId: [
        { required: true, message: "分类等级不能为空", trigger: "blur" }
      ],
      levelOrder: [
        { required: true, message: "组内不能为空", trigger: "blur" }
      ],
    }
  });

  const { queryParams, form, rules } = toRefs(data);

  /** 查询配置-证书目录列表 */
  function getList() {
    loading.value = true;
    listItem(queryParams.value).then(response => {
      itemList.value = proxy.handleTree(response.data, "id");
      loading.value = false;
    });
  }
  /** 展开/折叠操作 */
  function toggleExpandAll() {
    refreshTable.value = false;
    isExpandAll.value = !isExpandAll.value;
    nextTick(() => {
      refreshTable.value = true;
    });
  }
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }

  // 表单重置
  function reset() {
    form.value = {
      id: null,
      itemCode: null,
      itemName: null,
      parentId: null,
      levelId: null,
      levelOrder: null,
      icon: null,
      isLeaf: null,
      isShow: null,
      route: null,
      pathCode: null,
      pathName: null,
      remark: null
    };
    proxy.resetForm("itemRef");
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }

  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    listItem().then(response => {
      itemOptions.value = proxy.handleTree(response.data, "id");
    });
    mode.value = 'add';
    open.value = true;
    title.value = "添加配置-证书目录";
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    listItem().then(response => {
      itemOptions.value = proxy.handleTree(response.data, "id");
    });
    getItem(_id).then(response => {
      form.value = response.data;
      mode.value = 'update';
      open.value = true;
      title.value = "修改配置-证书目录";
    });
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["itemRef"].validate(valid => {
      if (valid) {
        if (form.value.id != null) {
          updateItem(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          form.value.id = form.value.itemCode
          addItem(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除配置-证书目录编号为"' + _ids + '"的数据项？').then(function() {
      return delItem(_ids);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('cer/item/export', {
      ...queryParams.value
    }, `item_${new Date().getTime()}.xlsx`)
  }

  getList();
</script>
